home *** CD-ROM | disk | FTP | other *** search
/ Packard Bell - Internet on a CD / internet on a cd.cdr / Internet / sites / Clementine_NASA / bitstrm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-16  |  3.6 KB  |  146 lines

  1. /*
  2.  *    THIS ROUTINE IS PART OF THE CLEMENTINE PDS FILE READER PROGRAM.
  3.  *    IT WAS WRITTEN BY ACT CORP. IN DIRECT SUPPORT TO THE 
  4.  *    CLEMENTINE (DSPSE) PROGRAM.
  5.  *
  6.  *    IF YOU FIND A PROBLEM OR MAKE ANY CHANGES TO THIS CODE PLEASE CONTACT
  7.  *    Dr. Erick Malaret at ACT Corp. 
  8.  *            tel: (703) 742-0294
  9.  *                 (703) 683-7431
  10.  *                       email:    nrlvax.nrl.navy.mil
  11.  *    
  12.  *
  13.  */
  14. #include <stdio.h>
  15. #include "jpeg_c.h"
  16.  
  17. void cBitStream( BitStream *bs, char *fn, Fmode fm )
  18. {
  19.     cByteStream( &bs->bytestream, fn, fm );
  20.     bs->BitBuffer = 0;
  21.     bs->bytesout = 0;
  22.     bs->outstring = NULL;
  23.     bs->BitBuffMask =  (fm==OUTPUT) ? 0x80:0x00;
  24.     bs->bitmask[0] = 0x0000; bs->bitmask[1] = 0x0001; bs->bitmask[2] = 0x0002;
  25.     bs->bitmask[3] = 0x0004; bs->bitmask[4] = 0x0008; bs->bitmask[5] = 0x0010;
  26.     bs->bitmask[6] = 0x0020; bs->bitmask[7] = 0x0040; bs->bitmask[8] = 0x0080;
  27.     bs->bitmask[9] = 0x0100; bs->bitmask[10] = 0x0200; bs->bitmask[11] = 0x0400;
  28.     bs->bitmask[12] = 0x0800; bs->bitmask[13] = 0x1000; bs->bitmask[14] = 0x2000;
  29.     bs->bitmask[15] = 0x4000; bs->bitmask[16] = 0x8000;
  30. }
  31.  
  32. void dBitStream(BitStream *bs)
  33. {
  34.     if ( bs->bytestream.mode == OUTPUT ) {
  35.         if ( bs->BitBuffMask != 0x80 ) {
  36.             while (bs->BitBuffMask) {
  37.                 bs->BitBuffer |= bs->BitBuffMask;
  38.                 bs->BitBuffMask >>= 1;
  39.             }
  40.             if ( bs->mode )
  41.                 bs->outstring[bs->bytesout] = bs->BitBuffer;
  42.             else
  43.                 ByteStream_write( &bs->bytestream, bs->BitBuffer );
  44.             bs->bytesout++;
  45.         }
  46.         if ( bs->mode ) {
  47.             if ( fwrite(bs->outstring,sizeof(char),bs->bytesout,bs->bytestream.file) == 0 )
  48.                 printf("Error: writing output bitstream to file.\n");
  49.         }
  50.         if ( fseek(bs->bytestream.file,8,0) )
  51.             printf("Error: fseek in subroutine dBitStream().\n");
  52.         else {
  53.             if ( fwrite(&(bs->bytesout),sizeof(long),1,bs->bytestream.file) == 0 )
  54.                 printf("Error: writing bytesout value to file.\n");
  55.         }
  56.     }
  57.     dByteStream(&bs->bytestream);
  58. }
  59.  
  60. short BitStream_write(BitStream *bs, short bits, short width)
  61. {
  62.     unsigned short BitMask = bs->bitmask[width];
  63.  
  64.     while ( BitMask ) {
  65.         if ( bits & BitMask ) bs->BitBuffer |= (short)bs->BitBuffMask;
  66.         BitMask >>= 1;
  67.         bs->BitBuffMask >>= 1;
  68.         if ( !bs->BitBuffMask ) {
  69.             if ( bs->mode )
  70.                 bs->outstring[bs->bytesout] = bs->BitBuffer;
  71.             else
  72.                 ByteStream_write( &bs->bytestream, bs->BitBuffer );
  73.             bs->bytesout++;
  74.             bs->BitBuffer = 0;
  75.             bs->BitBuffMask = 0x80;
  76.         }
  77.     }
  78.     return bs->bytestream.stat;
  79. }
  80.  
  81. short BitStream_read( BitStream *bs, short w )
  82. {
  83.     unsigned short RetVal = 0, BitMask = bs->bitmask[w];
  84.  
  85.     while ( BitMask ) {
  86.         if ( !bs->BitBuffMask ) {
  87.             if ( bs->mode ) {
  88.                 bs->BitBuffer = ((short)bs->outstring[bs->bytesout]) & 0x00ff;
  89.             } else
  90.                 bs->BitBuffer = ByteStream_read(&bs->bytestream);
  91.  
  92.             bs->bytesout++;
  93.             bs->BitBuffMask = 0x80;
  94.         }
  95.         if ( bs->BitBuffer & bs->BitBuffMask ) RetVal |= BitMask;
  96.         bs->BitBuffMask >>= 1;
  97.         BitMask >>= 1;
  98.     }
  99.     return RetVal;
  100. }
  101.  
  102. void cByteStream(ByteStream *Bs, char *FileName, Fmode FileMode)
  103. {
  104.     Bs->mode = FileMode;
  105.     if ( FileName != NULL ) {
  106.         Bs->file = fopen(FileName,(Bs->mode==INPUT) ? "rb":"wb");
  107.         if ( Bs->file == NULL ) printf("ByteStream constructor error.\n");
  108.         Bs->stat = 0;
  109.     }
  110. }
  111.  
  112. void dByteStream(ByteStream *Bs)
  113. {
  114.     if ( Bs->file ) {
  115.         fclose(Bs->file);
  116.         Bs->file = NULL;
  117.     }
  118. }
  119.  
  120. short ByteStream_read(ByteStream *Bs)
  121. {
  122.     short     c, d;
  123.     char    cval;
  124.         int    n;
  125.  
  126.     if ( Bs->mode == INPUT ) {
  127.         /* c = fgetc( Bs->file ); */
  128.         n=fread(&cval,sizeof(char),1,Bs->file);
  129.                 c    = cval; 
  130.         if ( n != 1 ) Bs->stat = EOF;
  131.         return c;
  132.     } else
  133.         Bs->stat = EOF;
  134. }
  135.  
  136. short ByteStream_write(ByteStream *Bs, short c)
  137. {
  138.     if ( (Bs->mode != OUTPUT) || (fputc(c,Bs->file) == EOF) ) Bs->stat = EOF;
  139.     return Bs->stat;
  140. }
  141.  
  142. short ByteStream_status(ByteStream *Bs)
  143. {
  144.     return Bs->stat;
  145. }
  146.